package com.tfc.glassesfree3dplayer;

import static com.tfc.glassesfree3dplayer.MainActivity.videoPath;

import android.content.pm.ActivityInfo;
import android.graphics.SurfaceTexture;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.view.Surface;
import android.view.TextureView;
import android.view.View;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.media3.common.MediaItem;
import androidx.media3.exoplayer.ExoPlayer;
import androidx.media3.ui.PlayerView;

import java.io.IOException;


public class PlayOnlineActivity extends AppCompatActivity implements TextureView.SurfaceTextureListener{
    private TextureView textureView;
    private OnlineVideoProcessor onlineVideoProcessor;
    private Surface surface;
    private MediaPlayer mMediaPlayer;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);

        //隐藏状态栏
        View decorView = getWindow().getDecorView();
        int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
        decorView.setSystemUiVisibility(uiOptions);

        // 设置为横屏
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        setContentView(R.layout.activity_play_online);
        //在开启了EdgeToEdge的情况下，防止内容与状态栏重叠
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.online_player_activity), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        textureView = findViewById(R.id.texture_view);
        textureView.setSurfaceTextureListener(this);
    }

    @Override
    public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int width, int height) {
        surface = new Surface(surfaceTexture);
        try {
            onlineVideoProcessor = new OnlineVideoProcessor();
            // 视频路径
            String videoPath = MainActivity.videoPath;
            onlineVideoProcessor.init(videoPath, surface);
            onlineVideoProcessor.startProcessing();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
        // 处理surface大小变化
    }

    @Override
    public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
        if (onlineVideoProcessor != null) {
            onlineVideoProcessor.stopProcessing();
        }
        return true;
    }

    @Override
    public void onSurfaceTextureUpdated(SurfaceTexture surface) {
        // surface更新时的回调
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (onlineVideoProcessor != null) {
            onlineVideoProcessor.stopProcessing();
        }
    }
}